package com.gomyb.admin.module.user.controller;

import com.gomyb.admin.module.user.model.SysPermission;
import com.gomyb.admin.module.user.model.SysRole;
import com.gomyb.admin.module.user.model.pojo.TransferPermission;
import com.gomyb.admin.module.user.service.SysPermissionService;
import com.gomyb.common.base.BaseController;
import com.gomyb.common.base.BaseService;
import com.gomyb.common.page.PageResult;
import com.gomyb.common.page.QueryFilter;
import com.gomyb.result.JsonResult;
import com.gomyb.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019-04-01 16:59:05
 */
@RestController
@Log4j2
@RequestMapping(value = "/sysRole", name = "角色管理")
public class SysRoleController extends BaseController<SysRole, Long> {

    @Autowired
    private SysPermissionService sysPermissionService;

    @Override
    @Autowired
    public void setService(BaseService<SysRole, Long> service) {
        super.service = service;
    }

    @PostMapping(value = "/see/{id}", name = "查看")
    public Result<SysRole> see(@PathVariable("id") Long id) {
        return super.get(id);
    }

    @PostMapping(value = "/add", name = "添加")
    public Result add(SysRole sysRole) {
        return super.save(sysRole);
    }

    @PostMapping(value = "/modify", name = "修改")
    public Result modify(SysRole sysRole) {
        return super.update(sysRole);
    }

    @PostMapping(value = "/list", name = "列表")
    public PageResult<SysRole> list(HttpServletRequest request) {
        return super.findPage(request);
    }

    @PostMapping(value = "/remove", name = "删除")
    public Result remove(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            SysRole role = service.get(Long.valueOf(id));
            super.delete(role);
        }
        return JsonResult.OK();
    }

    @PostMapping(value = "/findTransferAll", name = "获得全部权限")
    public Result<TransferPermission> findTransferAll() {
        return sysPermissionService.findTransferAll();
    }

    @PostMapping(value = "/findPermissionIdByRoleId", name = "获得某角色的全部权限")
    public Result<Long[]> findPermissionIdByRoleId(Long roleId) {
        SysRole sysRole = service.get(roleId);
        Set<SysPermission> permissionList = sysRole.getPermissionList();
        List<Long> list = new ArrayList<>(permissionList.size());
        permissionList.forEach(per -> {
            list.add(per.getId());
        });
        return JsonResult.OK(list.toArray());
    }

    @PostMapping(value = "/addPermission", name = "更新权限")
    public Result addPermission(String direction, String ids, Long roleId) {
        String[] idsStr = ids.split(",");
        try {
            SysRole sysRole = service.get(roleId);
            Set<SysPermission> permissionList = sysRole.getPermissionList();
            if (direction.equals("right")) {
                //添加权限
                for (String id : idsStr) {
                    SysPermission permission = sysPermissionService.get(Long.valueOf(id));
                    permissionList.add(permission);
                }
            } else {
                //移除权限
                List<String> idList = Arrays.asList(idsStr);
                permissionList.removeIf(sysPermission -> idList.contains(sysPermission.getId().toString()));
            }
            return super.save(sysRole);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResult.NO();
    }

}
